package jim.h.common.android.lib.zxing;

import jim.h.common.android.lib.zxing.integrator.IntentIntegrator;
import jim.h.common.android.lib.zxing.integrator.IntentResult;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 1、如果要采用在二维码中添加头像，那么生成的二维码最好采用最高等级H级别的纠错能力，
 * 目的有两个：一是增加二维码的正确识别能力；二是扩大二维码数据内容的大小。</br>
 * 2、头像大小最好不要超过二维码本身大小的1/5，而且只能放在正中间部位。
 * 这是由于二维码本身结构造成的。所以说新浪微博的二维码只是合理的利用了规则而已。</br>
 * 3、如果要仿照腾讯微信，在二维码边上增加装饰框，记得一定要在装饰框和二维码之间留出白边，这是为了二维码可识别。</br>
 * 
 * @author zhaohua
 * 
 */
public class TestActivity extends Activity implements OnClickListener {

    private EditText et_content;
    private ImageView img_qrcode;
    private TextView tx_scan_result;
    private Bitmap mtopBmp;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.test);

        et_content = (EditText) findViewById(R.id.et_content);
        img_qrcode = (ImageView) findViewById(R.id.img_qrcode);
        tx_scan_result = (TextView) findViewById(R.id.tx_scan_result);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_make_normal) {
            String text = et_content.getText().toString();
            if(TextUtils.isEmpty(text)) return;
            try {
                Bitmap bmp = QrcodeUtil.createNormalQrImg(text, 180);
                img_qrcode.setImageBitmap(bmp);
                mtopBmp = bmp;
            }catch (Exception e) {
                e.printStackTrace();
            }
        } else if (v.getId() == R.id.btn_make_logo) {
            String text = et_content.getText().toString();
            if(TextUtils.isEmpty(text)) return;
            
            try {
                Bitmap logo = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
                Bitmap bmp = QrcodeUtil.createLogoQrImg(text, 180, logo);
                img_qrcode.setImageBitmap(bmp);
                mtopBmp = bmp;
            }catch (Exception e) {
                e.printStackTrace();
            }
        } else if (v.getId() == R.id.btn_scan_here) {
            try {
                if(mtopBmp == null) return;
                String text = QrcodeUtil.decodeQrImg(mtopBmp);
                tx_scan_result.setText(text);
            }catch (Exception e) {
                e.printStackTrace();
            }
        } else if (v.getId() == R.id.btn_scan_camra) {
            IntentIntegrator.initiateScan(this,"QR_CODE", "utf-8", null);
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if(result != null) {
            tx_scan_result.setText(result.getContents());
        }
    }
}